package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/09/02 10:54
 **/
public class Day48_1_Num687_LongestUnivaluePath {
    class Solution {
        int maxLen = 0;
        public int longestUnivaluePath(TreeNode root) {
            if(root == null) return 0;
            order(root, root.val);
            return maxLen;
        }
        private int order(TreeNode root, int val){
            if(root == null) return 0;
            int left = order(root.left, root.val);
            int right = order(root.right, root.val);
            maxLen = Math.max(maxLen, left+right);
            if(root.val == val){
                return Math.max(left, right)+1;
            }
            return 0;
        }
    }
}